package com.dk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.dk.mapper.WarehouseMapper;
import com.dk.page.PageResult;
import com.dk.pojo.Warehouse;
import com.dk.pojo.WarehouseExample;
import com.dk.service.WarehouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PageResult<Warehouse> getAllwarehouse(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        String key = "getAllwarehouse";
        String sky = stringRedisTemplate.opsForValue().get(key);
        if (sky == null){//先判断redis有没有数据
            //redis没有数据 ，查询数据库
            List<Warehouse> warehouses = warehouseMapper.selectByExample(null);
            PageInfo<Warehouse> pageInfo=new PageInfo<>(warehouses);
            //存到redis
            stringRedisTemplate.opsForValue().set(key, JSONObject.toJSONString(warehouses));
            return new PageResult<>(pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getList());
        }
        //redis有数据 ， 直接获取redis的数据
        String s1 = stringRedisTemplate.opsForValue().get(key);
        List<Warehouse> warehouseList = JSONObject.parseArray(s1, Warehouse.class);/*转回list集合*/
        PageInfo<Warehouse> pageInfo1=new PageInfo<>(warehouseList);
        return new PageResult<>(pageInfo1.getPageNum(),pageInfo1.getPageSize(),pageInfo1.getTotal(),pageInfo1.getList());
    }

    @Override
    public void insertWarehouse(Warehouse warehouse) {
        int insert = warehouseMapper.insert(warehouse);
    }

    @Override
    public void deleteById(Integer id) {
        int i = warehouseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteAllById(List<String> ids) {
        warehouseMapper.deleteAllByIds(ids);
    }

    @Override
    public void updateWarehouse(Warehouse warehouse) {
        int i = warehouseMapper.updateByPrimaryKeySelective(warehouse);
    }

    @Override
    public Warehouse getById(Integer id) {
        return warehouseMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Warehouse> getwarehouseByName(String name) {
        WarehouseExample example=new WarehouseExample();
        WarehouseExample.Criteria criteria = example.createCriteria();
        if (!StrUtil.isEmpty(name) && !"".equals(name)){
            criteria.andWNameLike("%"+name+"%");
        }
        String key="getwarehouseByName";
        String sky = stringRedisTemplate.opsForValue().get(key);
        if (sky == null){
            //redis没有查询数据库
            List<Warehouse> warehouseList = warehouseMapper.selectByExample(example);
            //存redis
            stringRedisTemplate.opsForValue().set(key,JSONObject.toJSONString(warehouseList));//转json
            return warehouseList;
        }
        //redis有数据，直接取出
        String s1 = stringRedisTemplate.opsForValue().get(key);
//        List<Warehouse> warehouseList1= (List<Warehouse>) JSONUtils.parse(s1);BUG：不能这样强转  转换后的数据还是json
        List<Warehouse> warehouseList = JSONObject.parseArray(s1, Warehouse.class);
        //从json转回list集合
        System.out.println(warehouseList);
        String warehouseName=null;
        for (Warehouse warehouse : warehouseList) {
            warehouseName = warehouse.getwName();
        }
        //如果有数据  并且  name 数据库和redis的相等  就返回数据
        if (!name.equals(warehouseName)){
            //redis没有查询数据库
            List<Warehouse> warehouseList2 = warehouseMapper.selectByExample(example);
            //存redis
            stringRedisTemplate.opsForValue().set(key,JSONObject.toJSONString(warehouseList2));//转json
            return warehouseList2;
        }
        return warehouseList;
    }

    @Override
    public Warehouse getWarById(Integer warId) {
        return warehouseMapper.selectByPrimaryKey(warId);
    }
}
